<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="javax.servlet.http.*" %>
<%@page import="java.text.*" %>
<%@page import="java.util.Date" %>
<%@page import="nespresso.beans.*" %>
<%@page import="org.apache.commons.logging.*" %>
<html>

<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8"> 
<title>NESPRESSO</title>
<link rel="stylesheet" type="text/css" href="css/jquery-ui-1.8.17.custom.css" />
<link rel="stylesheet" type="text/css" href="css/ui.jqgrid.css" />
<link rel="stylesheet" type="text/css" href="css/nespresso.css" />

<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>

<script src="js/jquery.blockUI.js" type="text/javascript"></script>

<script src="js/grid.locale-fr.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.js" type="text/javascript"></script>

</head>

<body>
<div class="super-div">
<div id="divrond1">
	<table width="100%">
		<tr height="83px"><td width="50%"></td><td align="center">	
			<p class="grosblancitalique"><b>Nouvelle commande</b></p>
		</td></tr>
	</table>
</div>
<%
User currentUser = (User)session.getAttribute("user"); // On récupère le user de la session
request.removeAttribute("message");
Log log = LogFactory.getLog(this.getClass());
Date d = new Date();
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String currentDate = df.format(d);
// Pas d'attribut "commande dans la session : nouvelle commande !
Commande newCommande;
if(session.getAttribute("commande") == null) {
	newCommande = new Commande();
	session.setAttribute("commande", newCommande);
	newCommande.SetId("-NEW-");
	newCommande.SetUserresp(currentUser.getId());
	newCommande.SetDate(currentDate);
	newCommande.SetStatut("EN PREPARATION");
	newCommande.SetMontant(0);
	newCommande.SetLigne1(null);
	newCommande.SetPort(0);
} else { newCommande = (Commande)session.getAttribute("commande"); } %>
		
<form name="NCommande" action="Commandenew" class="f" method="post">
<div align="center">
	<b><font class="moyennoir">Responsable : </font>
    <font class="moyenblanc"><%=currentUser.getPrenom()+" "+currentUser.getNom() %></font>
    <font class="moyennoir"> - Date : </font><font class="moyenblanc"><%=newCommande.GetDate() %></font></b>
</div>
<div style="height:15px;display:block;"> </div>
<div align="center" class="divrondform" style="width:1100; margin:auto">
<table width="1090" cellpadding="0" cellspacing="0" height="549" align="center" border="0">
    <tr>
        <td width="862" height="489" valign="top"><p>
   			<table id="TabLigne">
    			<tr>
      				<th>Id</th>
      				<th>Destinataire</th>
   			   		<th>Capsule</th>
      				<th>Nombre</th>
     		 		<th>Montant</th>
  			  	</tr>
				<tbody>
     			<%	Ligne l = newCommande.GetLigne1();
       				Base bu = new Base(); Base bc = new Base();
      		 		int j; int lg;
       				String totligne;
     		 		while(l != null) {
    					bc.GetResultat("capsules", "id", l.GetIdcapsule()); bc.Suivant();
    		   			bu.GetResultat("users", "id", l.GetIduser()); bu.Suivant();
       					String nomprenom = new String(bu.GetTexte("prenom") + " " + bu.GetTexte("nom"));
     		  			String nomcapsule = new String(bc.GetTexte("nom"));
       					String smontant = new String(); smontant = String.valueOf(l.GetNbcapsule());
       					totligne = new DecimalFormat("0.00").format(l.GetNbcapsule() * bc.GetReel("prix"));
       					%>
       					<tr>
       						<td><%=l.GetId() %></td>
       						<td><%=nomprenom %></td>
       						<td><%=nomcapsule %></td>
       						<td><%=l.GetNbcapsule() %></td>
       						<td><%=totligne %></td>
       					</tr>
       					<% l = l.GetSuivante();
       				} 
       				bu.Free(); bc.Free();
       				%>
				</tbody>
			</table>
			<table>
				<tr>
					<td style="width:200px" class="moyenblanc">PORT&nbsp;:&nbsp;
					<input style="text-align:right" type="text" id="port" name="port" value="0" maxlength="5" size="4" />&nbsp;&#128;
					</td>
					<td align="center" class="moyenblanc" style="width:650px">Total de la commande&nbsp;:&nbsp;<%=new DecimalFormat("0.00").format(newCommande.GetMontant()) %>&#128;
					</td>
				</tr>
			</table>
        </td>
        <td height="549" valign="top" align="center">
            <p><input type="submit" id="Nouvelle ligne" name="Valide" value="Nouvelle ligne" class="boutons"></p>
            <p><input type="submit" id="Enreg" name="Valide" value="Créer la commande" class="boutons"></p>
            <p><input type="submit" name="Valide" value="Retour" class="boutons"></p>
        </td>
    </tr>
</table>
</div> 
<input type="hidden" id="nLigne" name="nLigne" value="NULL" />   
</form>
</div>
<script>

$.unblockUI();

$("input#Enreg").click(function() {
	$.blockUI({
		message:'<h1><img src="images/wait24trans.gif" /> Création de la commande en cours<br>Veuillez patienter...</h1>',
		css: { 
            border: 'none', 
            padding: '15px', 
            backgroundColor: '#000', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: .7, 
            color: '#fff' 
        }
	});
});

tableToGrid("#TabLigne", {
    autowidth: true,
    height: "500",
    hidegrid: false,
    onSelectRow: function(id) {
        $("#nLigne").val($("#TabLigne").getCell(id, 'Id'));
    },
    ondblClickRow: function(id) {
    	$("#nLigne").val($("#TabLigne").getCell(id, 'Id'));
    	$("#modif").click();
    }
});
</script>
</body>

</html>